package org.jground.rx.ui.zk.viewControl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXControl;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIStyles;
import org.jground.util.ArrayUtils;
import org.jground.util.StringUtils;
import org.jground.zk.zulX.DivX;
import org.jground.zk.zulX.HboxX;
import org.jground.zk.zulX.TableX;
import org.zkoss.image.AImage;
import org.zkoss.zhtml.Hr;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Toolbarbutton;

public class UIAttributeList extends UIAttributeBase {

    private static Log logger = LogFactory.getLog(UIAttributeControlCodetable.class);

    public UIAttributeList createNew(UIFrame uiFrame, RXView moControl) {
        return new UIAttributeList(uiFrame, moControl);
    }

    //
    public UIAttributeList() {}

    private UIFrame uiFrame;
    private Button addButton;

    private boolean isViewOnly;
    private ViewRenderer renderer;

    private UIAttributeList(UIFrame uiFrame, RXView moView) {

        super(uiFrame, moView);
        this.uiFrame = uiFrame;
        this.moView = moView;
        DivX ui = null;
        if (moView instanceof RXControl) {
            HboxX buttonPanel = UIHelper.createButtonPanel(addButton = new Button("Add"));
            ControlRenderer controlRenderer = new ControlRenderer();
            addButton.addEventListener(Events.ON_CLICK, controlRenderer);
            ui = new DivX(controlRenderer.getView(), buttonPanel);
            ui.setStyle("width:100%;border:red dotted 0px");
            renderer = controlRenderer;
            isViewOnly = false;
        } else {
            renderer = new ViewRenderer();
            ui = (DivX) renderer.getView();// new VboxX(renderer);
            isViewOnly = true;
        }
        super.addToControlComponent(ui);

    }

    public void set(String... values) {
        clearData();
        if (ArrayUtils.isNotEmpty(values)) {
            renderer.set(values);
        }
    }

    public IRXObject updateData() {
        if (isViewOnly) {
            return moDataObject;
        }
        moDataObject.set(super.rxAttribute.getName(), renderer.get());
        return moDataObject;
    }

    public void clearData() {
        renderer.clear();
    }

    public void setData(IRXObject moDataObject) {
        super.setData(moDataObject);
        renderer.setData(moDataObject);
    }

    private class ViewRenderer {

        private DivX view = new DivX();

        protected int currentCount;

        protected List<IUIAttributeView<Component, UIFrame>> uis = new ArrayList<IUIAttributeView<Component, UIFrame>>();

        public ViewRenderer(String... sizes) {
            // super(sizes);
            view.setStyle("width:100%;border:red 0px dotted");
        }

        public ViewRenderer() {
            this("");
        }

        public void set(String... values) {
            currentCount = values.length;
            addViewControl();
            for (int index = 0; index < currentCount; index++) {
                IUIAttributeView<Component, UIFrame> ui = uis.get(index);
                ui.setDataContext(moDataObject);
                String value = values[index];
                ui.set(value);
            }
            updateUI();
        }

        public void addViewControl() {
            while (currentCount >= this.uis.size()) {
                IUIAttributeView<Component, UIFrame> ui = UIAttributeViewControlFactory.create(uiFrame, moView);
                ui.clear();
                ui.setDataContext(moDataObject);
                uis.add(ui);
            }
        }

        public void removeViewControl(int index) {
            IUIAttributeView<Component, UIFrame> ui = uis.remove(index);
            ui.clear();
            uis.add(ui);
        }

        public String[] get() {
            logger.warn("this method should only be called in the control " + moView);
            return StringUtils.EMPTY_STRING_ARRAY;
        }

        public void setData(IRXObject mod) {
            for (IUIAttributeView<Component, UIFrame> ui : uis) {
                ui.setDataContext(mod);
            }
        }

        public void updateUI() {
            view.removeAll();
            for (int index = 0; index < currentCount; index++) {
                IUIAttributeView<Component, UIFrame> ui = uis.get(index);
                Hr hr = new Hr();
                hr.setStyle("border:1px gray solid;");
                view.add(new Hr());
                view.add(ui.getComponent());
            }
        }

        public void clear() {
            currentCount = 0;
            updateUI();
        }

        public Component getView() {
            return view;
        }

    }

    private class ControlRenderer extends ViewRenderer implements EventListener {

        private TableX view = new TableX("width:70%;text-align:left", "width:30%;text-align:left");

        private List<Button> deleteButtons = new ArrayList<Button>();

        public ControlRenderer() {
            super("80%", "10px");
        }

        public void addViewControl() {
            super.addViewControl();
            while (currentCount >= deleteButtons.size()) {
                // Button deleteButton = new Button();
                AImage img = UIStyles.getInstance().getAImage(UIStyles.ImageKey.delete16);
                Toolbarbutton deleteButton = new Toolbarbutton();
                deleteButton.setImageContent(img);
                deleteButton.addEventListener(Events.ON_CLICK, this);
                //deleteButton.setWidth("16px");
                //deleteButton.setHeight("16px");
                deleteButtons.add(deleteButton);
            }
        }

        public void onEvent(Event he) {
            // ADD
            Object target = he.getTarget();
            if (target == addButton) {
                currentCount++;
                addViewControl();
                updateUI();
                return;
            }
            // DELETE
            for (int index = 0; index < currentCount; index++) {
                Button button = deleteButtons.get(index);
                if (target == button) {
                    currentCount--;
                    this.removeViewControl(index);
                    updateUI();
                    return;
                }
            }

        }

        public String[] get() {
            List<String> values = new ArrayList<String>();
            for (int index = 0; index < currentCount; index++) {
                IUIAttributeView<Component, UIFrame> ui = uis.get(index);
                String[] vs = ((UIAttributeControlBase) ui).get();
                if (ArrayUtils.isNotEmpty(vs)) {
                    values.add(vs[0]);
                }
            }
            return values.toArray(StringUtils.EMPTY_STRING_ARRAY);
        }

        public void updateUI() {
            view.removeAll();
            for (int index = 0; index < currentCount; index++) {
                view.addRow(uis.get(index).getComponent(), deleteButtons.get(index));
            }
        }

        @Override
        public Component getView() {
            return view;
        }

    }

}
